#!/usr/bin/env python

import os, sys

def do(cmd):
    print repr(cmd)
    os.system(cmd)

def compress_file(fname):
    if fname.lower().endswith(".avi"):
	print "processing", fname
	basename = fname.rsplit(".", 1)[0]
	ifile = fname

	# mpg compression
	ofile = basename + ".mpg"
	if not os.path.exists(ofile):
	    cmd = "ffmpeg -i %s -sameq %s"%(ifile, ofile)
	    do(cmd)

	# flash compression
#	ofile = basename + ".flv"
#	if not os.path.exists(ofile):
#	    cmd = "ffmpeg -i %s -sameq %s"%(ifile, ofile)
#	    do(cmd)
    else:
	print "skipping", fname

def compress_dir(dname):
    for fname in filter(lambda x:x.lower().endswith(".avi"), os.listdir(dname)):
	compress_file(fname)

def main():
    for fname in sys.argv[1:]:
	if os.path.isdir(fname):
	    compress_dir(fname)
	elif os.path.isfile(fname):
	    compress_file(fname)
	else:
	    print "skipping", fname
	    pass
    #os.system("ls -lh *.AVI *.avi *.mpg")
    do("ls -lh *.AVI *.avi *.mpg")

if __name__ == '__main__':
    main()
